package org.ourgrid.acceptance.util.discoveryservice;

import java.util.List;

import org.easymock.classextension.EasyMock;
import org.ourgrid.acceptance.util.DiscoveryServiceAcceptanceUtil;
import org.ourgrid.common.interfaces.DiscoveryService;
import org.ourgrid.common.interfaces.DiscoveryServiceClient;
import org.ourgrid.discoveryservice.DiscoveryServiceComponent;
import org.ourgrid.matchers.WorkerProvidersMatcher;
import org.ourgrid.peer.PeerConstants;

import br.edu.ufcg.lsd.commune.container.ObjectDeployment;
import br.edu.ufcg.lsd.commune.container.logging.CommuneLogger;
import br.edu.ufcg.lsd.commune.context.ModuleContext;
import br.edu.ufcg.lsd.commune.identification.ContainerID;
import br.edu.ufcg.lsd.commune.identification.DeploymentID;
import br.edu.ufcg.lsd.commune.testinfra.AcceptanceTestUtil;

public class Req_507_Util extends DiscoveryServiceAcceptanceUtil {
	
	public Req_507_Util(ModuleContext context) {
		super(context);
	}
	
	public void getRemoteWorkerProviders(DiscoveryServiceComponent component, List<String> serviceIds,
			String clientName, String clientServerName) {
		
		DiscoveryService discoveryService = getDiscoveryServiceProxy();
		ObjectDeployment dsOD = getDiscoveryServiceControlDeployment(component);

		DeploymentID dscID = new DeploymentID(new ContainerID(clientName, clientServerName, 
				PeerConstants.MODULE_NAME, "dscPublicKey"), "DS_CLIENT");
		DiscoveryServiceClient dsc = EasyMock.createMock(DiscoveryServiceClient.class);
		
		dsc.hereIsRemoteWorkerProviderList(WorkerProvidersMatcher.eqMatcher(serviceIds));
			
		EasyMock.replay(dsc);
		
		AcceptanceTestUtil.publishTestObject(component, dscID, dsc, DiscoveryServiceClient.class);
		AcceptanceTestUtil.setExecutionContext(component, dsOD, dscID);

		discoveryService.getRemoteWorkerProviders(dsc);
			
		EasyMock.verify(dsc);
	}
	
	public void getNotLoggedRemoteWorkerProviders(DiscoveryServiceComponent component, String clientName, String clientServerName) {
		CommuneLogger logger = component.getLogger();
		
		logger.error("The client [" + clientName + "@" + clientServerName + "] is not logged.");
		EasyMock.replay(logger);
		
		DiscoveryService discoveryService = getDiscoveryServiceProxy();
		DeploymentID dscID = new DeploymentID(new ContainerID(clientName, clientServerName, 
				PeerConstants.MODULE_NAME, "dscPublicKey"), "DS_CLIENT");
		DiscoveryServiceClient dsc = EasyMock.createMock(DiscoveryServiceClient.class);
		AcceptanceTestUtil.publishTestObject(component, dscID, dsc, DiscoveryServiceClient.class);
		
		discoveryService.getRemoteWorkerProviders(dsc);
		
		EasyMock.verify(logger);
		EasyMock.reset(logger);
	}
	
}
